﻿using System;
using System.Drawing;
using System.Drawing.Text;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace TimeApp
{
    public partial class TimeForm : Form
    {
        [DllImport("user32.dll", EntryPoint = "ShowCursor", CharSet = CharSet.Auto)]
        public extern static void ShowCursor(int status);



        private string _ViewFormat;

        public TimeForm()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            this.DoubleBuffered = true;//避免修改LABEL闪烁
            this.TopMost = true;//最前显示
            this.FormBorderStyle = FormBorderStyle.None;//不显示标题栏和任务栏
            this.WindowState = FormWindowState.Maximized;//最大化

            //显示在第二个屏幕
            this.StartPosition = FormStartPosition.Manual;//很关键
            Screen[] screens = System.Windows.Forms.Screen.AllScreens;
            if (screens.Length >= 2)
            {
                //获取扩展屏
                Screen childerScreen = screens[1];
                //设置显示位置
                this.Location = childerScreen.WorkingArea.Location;
            }
            else
            {
                MessageBox.Show("只找到一个显示器，将在此显示器上显示！");
            }

            //定时器
            this.timer1.Tick += new System.EventHandler(this.Timer1_Tick);
            this.timer1.Start();

            //事件
            this.TimeLabel.MouseEnter += MainForm_MouseEnter;
            this.TimeLabel.MouseLeave += TimeForm_MouseLeave;
            this.MouseEnter += MainForm_MouseEnter;
            this.MouseLeave += TimeForm_MouseLeave;
        }


        private void Timer1_Tick(object sender, EventArgs e)
        {
            TimeLabel.Text = DateTime.Now.ToString(_ViewFormat, DateTimeFormatInfo.InvariantInfo);
        }


        private void MainForm_MouseEnter(object sender, EventArgs e)
        {
            ShowCursor(0);//隐藏鼠标
        }


        private void TimeForm_MouseLeave(object sender, EventArgs e)
        {
            ShowCursor(1);//显示鼠标
        }

        /// <summary>
        /// 设置显示样式
        /// </summary>
        /// <param name="FontPath"></param>
        /// <param name="FontSize"></param>
        public void SetViewFont(String ViewFormat, String FontPath, int FontSize, Color FontColor, Color BackColor)
        {
            //时间显示格式
            this._ViewFormat = ViewFormat;

            //字符设置
            PrivateFontCollection pfc = new PrivateFontCollection();
            pfc.AddFontFile(FontPath);
            //实例化字体
            Font f = new Font(pfc.Families[0], FontSize);
            //设置字体
            this.TimeLabel.Font = f;
            this.TimeLabel.ForeColor = FontColor;

            this.BackColor = BackColor;
        }

    }
}